package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private IStatementService statementService;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        if (appointment.getCustomerName()==null&&appointment.getCustomerPhone()==null&&appointment.getAppointmentTime()==null&&appointment.getLicensePlate()==null&&appointment.getCarSeries()==null&&appointment.getServiceType()==null){
            throw new BusinessException("参数异常");
        }

        appointment.setCreateTime(new Date());
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        appointment.setDelStatus(Appointment.DEL_ACTIVATION);
        appointment.setAppointmentChannel(Appointment.APPOINT_PHONE);
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment temp = appointmentMapper.selectByPrimaryKey(appointment.getId());
        if (temp == null) {
            throw new BusinessException("参数异常");
        }
        if(!Appointment.STATUS_APPOINTMENT.equals(temp.getStatus())){
            throw new BusinessException("非预约中状态不能修改");
        }
        appointmentMapper.updateAppointment(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void arrival(Long id) {
        if(id == null){
            throw new BusinessException("参数异常");
        }
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        if (appointment == null) {
            throw new BusinessException("参数异常");
        }
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("非预约中状态不能确认到店");
        }
        appointment.setActualArrivalTime(new Date());
        appointment.setStatus(Appointment.STATUS_ARRIVAL);
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void cancel(Long id) {
        if(id == null){
            throw new BusinessException("参数异常");
        }
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        if (appointment == null) {
            throw new BusinessException("参数异常");
        }
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("非预约中状态不能取消预约");
        }
        appointment.setStatus(Appointment.STATUS_CANCEL);
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteSoft(Long id) {
        if(id == null){
            throw new BusinessException("参数异常");
        }
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        if (appointment == null) {
            throw new BusinessException("参数异常");
        }
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("非预约中状态不能删除预约");
        }
        appointmentMapper.updateDelStatus(id, Appointment.DEL_FREEZE);
    }

    @Override
    public void changeStatus(Long appointmentId, Integer statusSettle) {
        appointmentMapper.changeStatus(appointmentId,statusSettle);
    }

    @Override
    public void saveAppoint(Appointment appointment) {
        appointment.setCreateTime(new Date());
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        appointment.setDelStatus(Appointment.DEL_ACTIVATION);
        appointment.setAppointmentChannel(Appointment.APPOINT_OFFICIAL);
        appointmentMapper.insert(appointment);
    }


    @Scheduled(cron = "0 0 12 * * ?")
    public void timeOut(){
        List<Appointment> appointments = appointmentMapper.selectAppointment();
        for (Appointment appointment : appointments) {
            long l = appointment.getAppointmentTime().getTime() - new Date().getTime();
            if(l <  0 ){
                appointmentMapper.changeStatus(appointment.getId(),Appointment.STATUS_OVERTIME);
            }
        }
    }
}
